package com.nightpiter.controller.algorithms;

import java.util.LinkedList;

/**
 * SortedVertexList class. a simple implementation of the sorted (by distance)
 * vertex list.
 * 
 * Package protected
 * 
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
@SuppressWarnings("serial")
class SortedVertexList extends LinkedList<Vertex> {

	private int pass;

	/**
	 * public constructor
	 */
	public SortedVertexList(int pass) {
		this.pass = pass;
	}

	/**
	 * @return the pass
	 */
	public int getPass() {
		return pass;
	}

	@Override
	public boolean add(Vertex vertex) {
		int i = 0;
		Way vertexWay = vertex.passWayMap.get(pass);
		for (Vertex element : this) {
			if (vertexWay.compareByDistance(element.passWayMap.get(pass)) < 0) {
				break;
			} else {
				i++;
			}
		}
		super.add(i, vertex);
		return true;
	}

	/**
	 * updates vertex position in the sorted list
	 * 
	 * @param vertex
	 */
	public void update(Vertex vertex) {
		this.remove(vertex);
		this.add(vertex);
	}
}
